using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MyGUI : MonoBehaviour {
	
	public bool startEnabled;
	public Camera mainCamera;
	public List<Camera> secondaryCameras;
	public List<GUIPanel> GUIpanels;
	
	public static int baseHeight = 768;
	public static int baseWidth = 1024;
	
	public GUIType GUItype;
	
	public enum GUIType{
		INVENTORY,
		CHARACTER,
		GAME,
		GAMEOVER,
		PAUSE
	}
	
	public enum ResizeType{
		MinStretch, 
		MaxStretch, 
		Normal,
	}
	void Start () {
		OnStart();
	}
	
	protected virtual void OnStart(){
		mainCamera = this.GetComponent<Camera>();
		ResizePanelsTo(Screen.width, Screen.height);
		camera.orthographicSize = camera.orthographicSize * (float)Screen.height / (float)baseHeight;
		SetGUIControllerReferences();
		gameObject.SetActive(startEnabled);
	}
	protected void SetGUIControllerReferences(){
		switch (GUItype){
		case GUIType.INVENTORY:
			InventoryController.GetInstance().SetInventoryGUI(this.gameObject);
			//InventoryController.GetInstance().ShowGUI(false);
			break;
		case GUIType.CHARACTER:
			AttributesController.GetInstance().SetCharacterGUI(this.gameObject);
            if (Screen.width / Screen.height < 1.5f)
            {
                this.transform.localScale -= Vector3.right * 0.25f;
            }
			break;
		}
	}
	
	Ray ray;
	RaycastHit raycastHitInfo;
	void Update () {
		OnUpdate();
	}
	protected virtual void OnUpdate(){
		int mouseButton = -1;
		if (Input.GetMouseButtonDown(0))
			mouseButton = 0;
		if (Input.GetMouseButtonDown(1))
			mouseButton = 1;
		if (mouseButton != -1){
			ray = mainCamera.ScreenPointToRay(Input.mousePosition);
		
			if (Physics.Raycast(ray, out raycastHitInfo) ){
				if (raycastHitInfo.collider.GetComponent<GUIPanel>() != null){
					switch(mouseButton){
					case 0:
						raycastHitInfo.collider.GetComponent<GUIPanel>().SendClic(raycastHitInfo.point);
//						Debug.Log(raycastHitInfo.point - raycastHitInfo.collider.GetComponent<GUIPanel>().transform.position);
						break;
					case 1:
						raycastHitInfo.collider.GetComponent<GUIPanel>().SendRightClic(raycastHitInfo.point);
						//Debug.Log(raycastHitInfo.point - raycastHitInfo.collider.GetComponent<GUIPanel>().transform.position);
						break;
					}
				} else if (GUItype == GUIType.INVENTORY){
					InventoryController.GetInstance().GetActiveInventory().OnClicOutside();	
				}
			}
		}
	}
	
	public void ResizePanelsTo(int width, int height){
		
		foreach (GUIPanel panel in GUIpanels){
			if (panel != null)
				panel.ResizeTo(width, height);
		}
	}
	
	
}
